﻿package mortal.game.view.common.menu
{
    import com.gengine.global.*;
    import fl.data.*;
    import fl.events.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.game.manager.*;
    import mortal.game.resource.info.item.*;

    public class ItemMenuRegister extends Object
    {
        private static var _itemList:ListMenu;
        private static var _layer:Sprite;
        private static var _currentActiveObject:InteractiveObject;
        private static var _currentItemData:ItemData = null;
        private static var _dicObjInfo:Dictionary = new Dictionary(true);
        private static var _dicObjdata:Dictionary = new Dictionary(true);

        public function ItemMenuRegister()
        {
            return;
        }// end function

        public static function register(param1:InteractiveObject, param2:ItemData, param3:DataProvider = null) : void
        {
            var _loc_4:DataProvider = null;
            if (!hasRegister(param1))
            {
                if (param3 != null)
                {
                    _loc_4 = param3;
                }
                else
                {
                    _loc_4 = ItemMenuConst.getDataProvider(param2);
                }
                _dicObjInfo[param1] = param2;
                _dicObjdata[param1] = _loc_4;
                param1.addEventListener(MouseEvent.CLICK, showInfo);
            }
            return;
        }// end function

        public static function unRegister(param1:InteractiveObject) : void
        {
            param1.removeEventListener(MouseEvent.CLICK, showInfo);
            delete _dicObjInfo[param1];
            delete _dicObjdata[param1];
            return;
        }// end function

        public static function hasRegister(param1:InteractiveObject) : Boolean
        {
            return _dicObjInfo[param1] ? (true) : (false);
        }// end function

        public static function hideOpList() : void
        {
            if (_itemList)
            {
                _itemList.hide();
            }
            return;
        }// end function

        private static function createItemList() : void
        {
            if (!_itemList)
            {
                _itemList = new ListMenu();
                _itemList.width = 84;
                _itemList.visible = false;
                _itemList.list.drawNow();
                _itemList.list.addEventListener(ListEvent.ITEM_CLICK, opItemSelect);
            }
            return;
        }// end function

        private static function showInfo(event:MouseEvent) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            if (!_layer)
            {
                _layer = LayerManager.topLayer;
            }
            createItemList();
            _currentActiveObject = event.currentTarget as InteractiveObject;
            _currentItemData = _dicObjInfo[_currentActiveObject];
            if (_currentActiveObject.parent)
            {
                _layer.addChild(_itemList);
                _itemList.dataProvider = ItemMenuConst.getEnabeldAttri(_dicObjdata[_currentActiveObject], _dicObjInfo[_currentActiveObject]);
                _loc_2 = event.stageX;
                _loc_3 = event.stageY;
                _loc_4 = 5;
                _loc_2 = _loc_2 < _layer.stage.stageWidth - _itemList.width - _loc_4 ? (_loc_2) : (_loc_2 - _itemList.width - _loc_4);
                _loc_3 = _loc_3 < _layer.stage.stageHeight - _itemList.height ? (_loc_3) : (_loc_3 - _itemList.height);
                _itemList.show(_loc_2, _loc_3, _layer);
            }
            Global.stage.addEventListener(MouseEvent.CLICK, hideHandler);
            event.stopImmediatePropagation();
            return;
        }// end function

        private static function hideHandler(event:MouseEvent) : void
        {
            hideOpList();
            Global.stage.removeEventListener(MouseEvent.CLICK, hideHandler);
            return;
        }// end function

        private static function opItemSelect(event:ListEvent) : void
        {
            ItemMenuConst.opearte(_itemList.list.dataProvider.getItemAt(event.index)["label"], _currentItemData);
            return;
        }// end function

    }
}
